package com.tepth.maintenancesystem.bean;

import android.util.Log;


import com.tepth.maintenancesystem.untils.AESUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by LHQ on 2016/11/18.
 */

public class User {
    private String mId;
    private String mPwd;
    private String authKey;
    private String loginName;
    private String personCode;
    private String personName;
    private String orgName;
    private String deptName;
    private String groupName;
    private String jobName;
    private static final String masterPassword = "FORYOU"; // AES加密算法的种子
    private static final String JSON_ID = "user_id";
    private static final String JSON_PWD = "user_pwd";
    private static final String JSON_AUTHKEY="user_authkey";
    private static final String JSON_LOGINNAME="user_loginname";
    private static final String JSON_PERSONCODE="user_personcode";
    private static final String JSON_PERSONNAME="user_personname";
    private static final String JSON_ORGNAME="user_orgname";
    private static final String JSON_DEPTNAME="user_deptname";
    private static final String GROUPNAME="user_groupname";
    private static final String JOBNAME="user_jobname";
    private static final String TAG = "User";

    public User(String id, String pwd) {
        this.mId = id;
        this.mPwd = pwd;
    }

    public User(JSONObject json) throws Exception {
        if (json.has(JSON_ID)) {
            String id = json.getString(JSON_ID);
            String pwd = json.getString(JSON_PWD);
            // 解密后存放
            mId = AESUtils.decrypt(masterPassword, id);
            mPwd = AESUtils.decrypt(masterPassword, pwd);
            authKey=json.getString(JSON_AUTHKEY);
            loginName=json.getString(JSON_LOGINNAME);
            personCode=json.getString(JSON_PERSONCODE);
            personName=json.getString(JSON_PERSONNAME);
            orgName=json.getString(JSON_ORGNAME);
            deptName=json.getString(JSON_DEPTNAME);
            groupName=json.getString(GROUPNAME);
            jobName=json.getString(JOBNAME);
        }
    }

    public User(User user){
        this.mId=user.getId();
        this.mPwd=user.getPwd();
        this.authKey=user.getAuthKey();
        this.loginName=user.getLoginName();
        this.personCode=user.getPersonCode();
        this.personName=user.getPersonName();
        this.orgName=user.getOrgName();
        this.deptName=user.getDeptName();
        this.groupName=user.getGroupName();
        this.jobName=user.getJobName();
    }

    public JSONObject toJSON() throws Exception {
        // 使用AES加密算法加密后保存
        String id = AESUtils.encrypt(masterPassword, mId);
        String pwd = AESUtils.encrypt(masterPassword, mPwd);
        JSONObject json = new JSONObject();
        try {
            json.put(JSON_ID, id);
            json.put(JSON_PWD, pwd);
            json.put(JSON_AUTHKEY,authKey);
            json.put(JSON_LOGINNAME,loginName);
            json.put(JSON_PERSONCODE,personCode);
            json.put(JSON_PERSONNAME,personName);
            json.put(JSON_ORGNAME,orgName);
            json.put(JSON_DEPTNAME,deptName);
            json.put(GROUPNAME,groupName);
            json.put(JOBNAME,jobName);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json;
    }

    public String getId() {
        return mId;
    }

    public String getPwd() {
        return mPwd;
    }

    public String getAuthKey(){
        return authKey;
    }

    public void setmId(String mId) {
        this.mId = mId;
    }

    public void setmPwd(String mPwd) {
        this.mPwd = mPwd;
    }

    public void setAuthKey(String authKey) {
        this.authKey = authKey;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getJobName() {
        return jobName;
    }

    public void setJobName(String jobName) {
        this.jobName = jobName;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public String getOrgName() {
        return orgName;
    }

    public void setOrgName(String orgName) {
        this.orgName = orgName;
    }

    public String getPersonName() {
        return personName;
    }

    public void setPersonName(String personName) {
        this.personName = personName;
    }

    public String getPersonCode() {
        return personCode;
    }

    public void setPersonCode(String personCode) {
        this.personCode = personCode;
    }
}
